﻿/*******************************************************************\
| Author:    Andrew Gamez agamez2009@my.fit.edu                     |
| Date:      Nov 16, 2011                                           |
| Class:     GUIemm                                                 |
| Descr:     Objects related to EMMs.                               |
\*******************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SaveAWatt.EMMObjects
{
    public class GUIemm
    {
        private string m_name; // The name that identifies an EMM in the GUI's listbox.
        private string m_id; // The underlying identifier for this EMM object.
        private List<double> m_lifeReadings; // Stores readings that factor in to average since EMM object's creation.
        private List<DateTime> m_lifeReadingTimes; // Stores readings that factor in to average since EMM object's creation.
        private double m_average;

        public GUIemm(string idUponConstruction)
        {
            m_id = idUponConstruction;
            m_name = "";
            m_lifeReadings = new List<double>();
            m_lifeReadingTimes = new List<DateTime>();
            m_average = 0;
        }

        public override string ToString()
        {
            if (m_name.CompareTo("") != 0)
            {
                return getName();
            }
            else
            {
                return getID();
            }
        }

        public void setName(string name)
        {
            this.m_name = name;
        }

        public string getName()
        {
            return m_name;
        }

        public string getID()
        {
            return m_id;
        }

        public List<double> getReadings()
        {
            return m_lifeReadings;
        }

        /**
         * Calculates the average of readings between two dates.
         */
        public double calculateAverageInBounds(DateTime minBound, DateTime maxBound)
        {
            double sum = 0;
            int totalReadings = 0;

            for (int i = 0; i < m_lifeReadings.Count; i++)
            {
                DateTime readingTime = m_lifeReadingTimes[i];
                if (readingTime.CompareTo(minBound) >= 0
                    && readingTime.CompareTo(maxBound) <= 0)
                {
                    totalReadings++;
                    sum += m_lifeReadings[i];
                }
            }

            if (totalReadings == 0)
            {
                return 0;
                System.Console.WriteLine("here");
            }
            else
            {
                return (sum / totalReadings);
            }
        }

        /**
         * A way to convert a DateTime to a double.
         */
        public static double timeToDouble(DateTime x)
        {
            return Convert.ToSingle(x.ToOADate()); ;
        }

        public List<DateTime> getTimes()
        {
            return m_lifeReadingTimes;
        }

        public double setUsage(double newUsage, DateTime usageTime)
        {
            m_lifeReadings.Add(newUsage);
            m_lifeReadingTimes.Add(usageTime);
            double sum = 0;
            for (int i = 0; i < getReadings().Count; i++)
            {
                sum += getReadings()[i];
            }
            m_average = sum / getReadings().Count;
            return m_average;
        }

        public double getUsage()
        {
            if (m_lifeReadings.Count == 0)
            {
                return 0;
            }
            else
            {
                return m_lifeReadings[m_lifeReadings.Count - 1];
            }
        }

        public double getAverage()
        {
            return m_average;
        }

        public void removeFromGUI()
        {
            // Call RemoveEMM(id);
            // it will have proper functionality later; right now it only returns true.
        }
    }
}
